<?php


class yswarranty_ctl_admin_mywarranty extends desktop_controller
{
    public $workground = "yswarranty_ctl_admin_mywarranty";
    //public $workground = "ectools_ctl_admin_order";
    public $use_buildin_import = false;

    public function __construct( $app )
    {
        parent::__construct($app);
        $this->app = $app;
        header( "cache-control: no-store, no-cache, must-revalidate" );
        $this->obj = $this;
    }


     
    public function listproducttypes()
    {
        $this->finder( "yswarranty_mdl_productstyle",
                        array(
                                "title" => app::get( "desktop" )->_( "商品機種管理" ),
                                "actions" => array(
                                        array(
                                                "label" => app::get( "b2c" )->_( "添加機種" ),
                                                "href" => "index.php?app=yswarranty&ctl=admin_mywarranty&act=newproducttypedlg",
                                                "target" => "dialog::{ title:'".app::get( "yswarranty" )->_( "添加機種" )."', width:800, height:540 }"
                                        )
                                ),
                                'use_buildin_filter'=>true,
                                'use_buildin_refresh'=>true,
                                'use_buildin_export'=>true
                        )
        );
    }
    public function newproducttypedlg()
    {
        $this->page( "admin/producttype/newproducttypedlg.html");
    }
    public function newproducttype()
    {
        $this->page( "admin/producttype/newproducttype.html");
    }
     
    /*************************************************************************************
     *
    *
    *************************************************************************************/
    public function listvalidwarranties()
    {
        $this->finder( "yswarranty_mdl_validwarranties",
                        array(
                                "title" => app::get( "desktop" )->_( "有效保證書編號" ),
                                "actions" => array(
                                        array(
                                                "label" => app::get( "b2c" )->_( "新增保證書編號" ),
                                                "href" => "index.php?app=yswarranty&ctl=admin_mywarranty&act=newvalidwarranties",
                                                "target" => "dialog::{ title:'".app::get( "yswarranty" )->_( "新增保證書編號" )."', width:600, height:240 }"
                                        )
                                ),
                                "use_buildin_recycle" =>true,
                                'use_buildin_filter'=>true,
                                'use_buildin_refresh'=>true,
                                'use_buildin_export'=>true,
                        )
        );
    }
    public function newvalidwarranties()
    {
         
        $mdl=app::get("yswarranty")->model("productstyle");
        $data = $mdl->getList( 'product_style',array(),0,'100000','product_style asc');
        $styles=array();
        foreach ($data as $k)
        {
            $styles[]=$k['product_style'];
        }

        $this->pagedata['vv']=serialize($data);
        $this->pagedata['product_styles']=$styles;
        $this->page( "admin/validwarranties/newvalidwarranty.html");
    }
    public function addvalidwarranty()
    {


        header( "Content-Type:text/jcmd; charset=utf-8" );
        
        $pmdl=app::get("yswarranty")->model("productstyle");
        $ps = $pmdl->getList( 'product_style',array(),0,'100000','product_style asc');

        $id=trim($_POST['product_style']);
        $id=intval($id);
        
        $product_style=$ps[$id]['product_style'];
        $product_no=trim($_POST['product_no']);
        $warranty_no=trim($_POST['warranty_no']);
        $verify_code=trim($_POST['verify_code']);
        
  
        if(empty($product_style))
        {
            echo "{success:\"保存失敗~無效的機種~~~\",_:null,toclear:true }";
            exit();
        }
      
        if(empty($warranty_no))
        {
            echo "{success:\"保存失敗成功~~~~保證書編號不可為空\",_:null,toclear:true }";
            exit();
        }
        //看是否存在~~
        $mdl=app::get("yswarranty")->model("validwarranties");
        $filter=array("warranty_no"=>$warranty_no);
        
        $exists=$mdl->count($filter);
        
        

        if($exists==0)
        {
            $toinsert=array('product_style'=>$product_style,
                                'product_no'=>$product_no,
                                'warranty_no'=>$warranty_no,
                                'verify_code'=>$verify_code,
                                'createat'=>time(),
                                'used'=>'N',
                                'valid'=>'Y');
            
            $seri=serialize($toinsert);
            if($mdl->insert($toinsert)!=FALSE)
            {
                echo "{success:\"新增成功,  可以輸入下一條~~~~\",_:null,toclear:false }";                
            }else
            {
                echo "{success:\"新增失敗~~~~\",_:null,toclear:false }";
            }
        }else
        {

            $toinsert=array('product_style'=>$product_style,
                                    'product_no'=>$product_no,
                                    'verify_code'=>$verify_code);
            //update
            $r=$mdl->update($toinsert,$filter);
            
            if($r!=FALSE)
            {
                echo "{success:\"更新成功,可以輸入下一條~~~~\",_:null,toclear:false }";
            }else
            {
                echo "{success:\"更新失敗,可以輸入下一條~~~~\",_:null,toclear:false }";
            }
        }
         
    }
    public function importvalidwarranties()
    {
        kernel::log("importvalidwarranties");
        $this->page( "admin/validwarranties/warranty_import.html");
    }
    /*********************************************************
     *
    *
    *********************************************************/
    public function do_upload_validtwarranty()
    {
        kernel::log("to_importvalidtwarranty");
        echo "<script>top.MessageBox.error('".app::get( "express" )->_( "无法读取结构信息,模板包可能已损坏" )."');</script>";
        exit();
        //$this->page( "admin/producttype/newproducttype.html");
         
        if ($_FILES["import_file"]["error"] > 0)
        {
            kernel::log("error");
        }
        $targetFile=$_FILES["import_file"]["tmp_name"]. $_FILES["import_file"]["name"];
        move_uploaded_file($_FILES["import_file"]["tmp_name"], $targetFile);
        kernel::log($targetFile);

        $this->pagedata['uploadfile']=$targetFile;
        //     $_excelobj=$factory->getMapReader();
        //     $_excelobj->Load($targetFile);
        //      $maprowIterator=$_excelobj->getMapRowIterator();

        // echo "<script> alert('hello');</script>";

        /*
         $mdl=app::get("yswarranty")->model("validwarranties");
        $this->begin( "index.php?app=yswarranty&ctl=admin_mywarranty&act=listvalidwarranties" );
        try {
        foreach($maprowIterator as $row)
        {
        $row['createat']=time();
        $row['used']="N";
        $row['valid']="Y";
        $mdl->insert($row);
        }
        }catch(Exception $e)
        {
        $this->end(FALSE,  app::get( "yswarranty" )->_( "失敗" ) );
        }
        $this->end(TRUE,  app::get( "yswarranty" )->_( "操作成功" ) );
        */
    }
    /*******************************************************************
    *     會員保證卡查詢
    *
    *
    ********************************************************************/
    public function warrantyofmember()
    {
        
        $this->finder( "yswarranty_mdl_warranty_reg",
                        array(
                                "title" => app::get( "desktop" )->_( "已註冊保證卡查詢" ),
                                'use_buildin_filter'=>true,
                                'use_buildin_refresh'=>true,
                                'use_buildin_export'=>true
                        )
        );        
    }
    /*******************************************************************
     *
     * 
     * 
     ********************************************************************/
    public function addproducttypex()
    {
        $stylestr=$_POST['product_style'];
         
        $mdl=app::get("yswarranty")->model("productstyle");
        $this->begin( "index.php?app=yswarranty&ctl=admin_mywarranty&act=listproducttypes" );

        if($stylestr)
        {
            $styles = explode("\n", $stylestr);

            foreach($styles as $s)
            {
                $s=trim($s);
                if(empty($s))
                    continue;
                $keywords = preg_split("/[\s\t]+/", $s);
                if(count($keywords)==2)
                {
                    $data=array('product_style'=>trim($keywords[1]));
                    $c=$mdl->count($data);

                    if($c>0)
                    {
                        $mdl->update(array('product_category'=>trim($keywords[0])),$data);
                    }else
                    {
                        $data=array('product_category'=>trim($keywords[0]),'product_style'=>trim($keywords[1]));
                        $mdl->insert($data);
                    }
                }
            }
        }
        $this->end(TRUE,  app::get( "yswarranty" )->_( "操作成功" ) );
    }
    public function settings( )
    {
        if ( !$_POST )
        {
            $this->pagedata['return_product']['is_open'] = app::get('yswarranty')->getConf( "site.is_open_yswarranty" );
            $this->pagedata['return_product']['comment'] = app::get('yswarranty')->getConf( "site.yswarranty_comment" );
            $this->page( "admin/setting/warrantysettings.html" );
        }
        else
        {
            $this->begin( "index.php?app=yswarranty&ctl=admin_mywarranty&act=settings" );
            if ( $_POST['return_is_open'] == "true" )
            {
                app::get('yswarranty')->setConf( "site.is_open_yswarranty", 1 );
            }
            else
            {
                app::get('yswarranty')->setConf( "site.is_open_yswarranty", 0 );
            }
            app::get('yswarranty')->setConf( "site.yswarranty_comment", $_POST['conmment'] );
            $this->end( true, app::get( "yswarranty" )->_( "设置成功！" ) );
        }
    }
}
?>